﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Three.BookStore.SystemSettings;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;

namespace Three.BookStore.RBAC
{
    /// <summary>
    /// 用户
    /// </summary>
    public class Users
    {
        private readonly IRepository<UserModel> _userServices;

        public Users(IRepository<UserModel> userServices)
        {
            _userServices = userServices;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<int> AddUser(UserModel user)
        {
            UserModel users = new UserModel()
            {
                UserName = user.UserName,
                UserPwd = user.UserPwd,
                UserNickName = user.UserName,
            };
            var usercount = await _userServices.InsertAsync(users);
            return usercount == null ? 0 : 1;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<int> DeleteUser(int userId)
        {
            var result = _userServices.DeleteDirectAsync(m => m.Id == userId);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 反填用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<UserModel> GetUser(int userId)
        {
            var result = await _userServices.FindAsync(m => m.Id == userId);
            return result;

        }

        /// <summary>
        /// 显示用户
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserModel>> ShowUser()
        {
            var userList = await _userServices.GetListAsync();
            return userList;
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task<int> UpdateUser(UserModel user)
        {
            UserModel users = new UserModel()
            {
                UserName = user.UserName,
                UserPwd = user.UserPwd,
                UserNickName = user.UserName,
            };
            var usercount = await _userServices.UpdateAsync(user);
            return usercount == null ? 0 : 1;
        }
    }
}
